<?php

namespace app\foreigndata\controller;

use app\common\controller\Api;
use addons\epay\library\Service;
use think\Db;

/**
 * 首页接口
 */
class Vxpay extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    // public function __construct(){
    //     $this->config = Service::getConfig('wechat');
    //     $this->appid = $this->config['app_id'];
    //     $this->secret = $this->config['app_secret'];
    //     $this->merch_id = $this->config['mch_id'];
    // }
    
    public function index(){
        // var_dump($this->config);die;
        include_once realpath(__DIR__ ."/../../../addons/vxpay/example/phpqrcode/phpqrcode.php");
        $errorLevel = "L";
        $size = "6";
        $str = $_GET['str'];
        $id = $_GET['id'];
        $type = $_GET['type'];
        $url="https://ceshi.780g.com/BfcTyWlLqd.php/vxpay/pay/jsapipay?str=".$str."&id=".$id."&type=".$type;
        // $url= 'a';
        \QRcode::png($url,'qrcode.png', $errorLevel, $size,2);
        
        $logo = "ww.png";//准备好的logo图片 
        $QR = 'qrcode.png';//已经生成的原始二维码图 
        if ($logo !== FALSE) { 
         $QR = imagecreatefromstring(file_get_contents($QR)); 
         $logo = imagecreatefromstring(file_get_contents($logo)); 
         $QR_width = imagesx($QR);//二维码图片宽度 
         $QR_height = imagesy($QR);//二维码图片高度 
         $logo_width = imagesx($logo);//logo图片宽度 
         $logo_height = imagesy($logo);//logo图片高度 
         $logo_qr_width = $QR_width / 5; 
         $scale = $logo_width/$logo_qr_width; 
         $logo_qr_height = $logo_height/$scale; 
         $from_width = ($QR_width - $logo_qr_width) / 2; 
         //重新组合图片并调整大小 
         imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
         $logo_qr_height, $logo_width, $logo_height); 
        } 
        //输出图片 
        Header("Content-type: image/png");
        ImagePng($QR);
        
    // 	return '<img src="/qrcode.png" alt="使用微信扫描支付">'; 
        
    }
    
    public function getEwr(){
        $name = 'tupian'.rand(1,99).'.png';
        // return '<img src="https://ceshi.780g.com/foreigndata/vxpay/index?name='.$name.'" alt="使用微信扫描支付">';
        $str = $this->request->post("rand_str");
        $id = $this->request->post("id");
        $type = $this->request->post("type");
        
        //存到数据库
        $data = array(
            'order_id'=>$id,
            'rand'=>$str,
            'type'=>$type,
            'create_time'=>date("Y-m-d H:i:s",time()),
            'uid'=>1
        );
        DB::name("order_prepare")->insert($data);
        
        $info = DB::name("order")->where("id",$id)->field("status")->find();
        if($info['status'] != 1){
            $code = 1;
        }else{
            $code = 0;
        }
        
        $data = array(
            'url'=>'https://ceshi.780g.com/foreigndata/vxpay/index?str='.$str.'&id='.$id."&type=".$type,
            'status'=>$code,
        );
        
        $this->success("success",$data);
    }
    
    public function notify_url(){
        echo 1;die;
        // error_log("wxpay_notify".date("Y-m-d H:i"),3,'vxpay.log');
    }
    
    public function curl_get($url){
        
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
     
        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
     
        // 设置请求头
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);
     
        // 显示错误信息
        if (curl_error($curl)) {
            print "Error: " . curl_error($curl);
        } else {
            // 打印返回的内容
            return json_decode($data);
        }
    }

}
